import Mock from "mockjs";
import { param2Obj } from "../utils/url";

let List = [];
for (let i = 0; i < 200; i++) {
  List.push({
    id: Mock.mock("@id()").substring(0, 10),
    date: Mock.mock("@date()"),
    name: Mock.mock("@cname()"),
    phone: Mock.mock("@zip()"),
    payMoney: Mock.mock("@integer(500, 10000)"),
    num: Mock.mock("@integer(0, 30)"),
    classData: Mock.mock("@integer(1,6)"),
    payData: Mock.mock("@date()"),
    value: false,
  });
}
export default {
  // 请求数据
  getTeacherhome: (config) => {
    let { search, pageSize, page, date1, date2 } = param2Obj(config.url);
    if (date1 == "" && date2 == "") {
      var newList = List.filter((item) => {
        return (
          item.name.indexOf(search) != -1 || item.phone.indexOf(search) != -1
        );
      });
    } else {
      var newList = List.filter((item) => {
        return (
          (item.name.indexOf(search) != -1 ||
            item.phone.indexOf(search) != -1) &&
          item.payData > date1 &&
          item.payData < date2
        );
      });
    }
    let newList2 = newList.filter((item, index) => {
      return (page - 1) * pageSize <= index && page * pageSize > index;
    });
    return {
      code: 200,
      msg: "请求成功",
      data: newList2,
      total: newList.length,
    };
  },
  // 删除
  deleteData: (config) => {
    let { id } = JSON.parse(config.body);
    List = List.filter((item) => {
      return item.id != id;
    });
    return {
      code: 200,
      msg: "删除成功",
      data: null,
    };
  },
  //数据修改
  changeData: (config) => {
    const { id, name, phone, payMoney, num, classData } = JSON.parse(
      config.body
    );
    List.forEach((item) => {
      if (item.id == id) {
        item.name = name;
        item.phone = phone;
        item.payMoney = payMoney;
        item.num = num;
        item.classData = classData;
      }
    });
    return {
      code: 200,
      msg: "修改成功",
      data: null,
    };
  },
  //批量删除
  batchData: (config) => {
    let { ids } = JSON.parse(config.body);
    List = List.filter((item) => {
      return ids.indexOf(item.id) === -1;
    });
    return {
      code: 200,
      msg: "批量删除成功",
      data: null,
    };
  },
};
